#cmake file for project libexample
#author:guyadong
#created:2022/01/14

cmake_minimum_required( VERSION 3.0 )
if(POLICY CMP0048)
  cmake_policy(SET CMP0048 NEW)
endif()
if(POLICY CMP0077)
  cmake_policy(SET CMP0077 NEW)
endif()
if(POLICY CMP0074)
  cmake_policy(SET CMP0074 NEW)
endif()
# 3.0以上版本才允许使用VERSION option
project(libexample VERSION 1.2.1.1 LANGUAGES C)
# 要求C编译器支持C99
set(CMAKE_C_STANDARD 99)
SET(CMAKE_C_STANDARD_REQUIRED ON)
# 是否编译动态库
option(BUILD_SHARED_LIBS "create shared libraries if on" OFF)

# 优先使用环境变量定义的BUILD_SHARED_LIBS
if(DEFINED ENV{BUILD_SHARED_LIBS})
	MESSAGE(STATUS "DEFINE BUILD_SHARED_LIBS=$ENV{BUILD_SHARED_LIBS}")
	set(BUILD_SHARED_LIBS $ENV{BUILD_SHARED_LIBS})
endif()
# for generators = cmake_find_package
# generators 为 cmake_find_package 时会在 CMAKE_BINARY_DIR 下为所有依赖库生成对应FindXXXX.cmake脚本
list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
if(MSVC)
	#关闭C4819警告
	add_definitions("/wd4819")
	message(STATUS "optional:/wd4819")
	#关闭CRT_SECURE警告
	add_definitions(-D_CRT_SECURE_NO_WARNINGS)
	message(STATUS "optional:-D_CRT_SECURE_NO_WARNINGS")
endif(MSVC)
# 在支持层次工程组织的IDE(MSVC)中使用"FOLDER"属性定义的文件夹名称来组织项目
set_property(GLOBAL PROPERTY USE_FOLDERS ON)


##############设置目标文件生成位置#####################
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
# define RUNTIME_INSTALL_DIR LIBRARY_INSTALL_DIR INCLUDE_INSTALL_DIR CONFIG_INSTALL_DIR
set (RUNTIME_INSTALL_DIR bin)
set (LIBRARY_INSTALL_DIR lib)
set (INCLUDE_INSTALL_DIR include)

set( SUBDIRECTORIES src)
foreach( subdir ${SUBDIRECTORIES} )
	if( IS_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/${subdir}" )
		MESSAGE( STATUS "Found sub project ${subdir}, adding it" )
		add_subdirectory( ${subdir} )
	endif()
endforeach()
